<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2017/2/21
 * Time: 17:06
 */

namespace Application\UserCenter\Controller;


use J\Exception;
use J\Intent;
use J\Message;
use J\MVC\Controller;

/**
 * Use 内部服务类，响应Intent调用
 * 服务于其他应用调用
 */
class InnerController extends Controller
{
    /**
     * 根据ID列表获取相应昵称
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionNames(Intent $intent)
    {
        $rs = $this->module->getModels()->getCollection('user')->getCurd()
            ->setElements(['nickname'])
            ->setIds($intent->paramUid)
            ->select();
        $r = [];
        foreach ($rs as $id => $row) {
            $r[(int)$id] = $row['nickname'];
        }
        $intent->value = $r;
    }

    /**
     * 根据昵称获取用户ID
     * @param Intent $intent
     * @return bool
     * @throws Exception
     */
    protected function optionId(Intent $intent)
    {
        $rs = $this->module->getModels()->getCollection('user')->getCurd()
            ->setWhere(['nickname' => $intent->paramName])
            ->getIds();

        $intent->value = $rs[0];
        return true;
    }

    /**
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionFetchResource(Intent $intent)
    {
        if (!$this->getApp()->User()->isAdministrator()) {
            throw new Exception(Message::SUPER_MUST);
        }

        $this->module->insertValuesToCollection('resource', $intent->rows);
    }
}